<script>
import storage from '@/utils/storage.js'
import {
  SYSTEM_CLIENT,
  CHANNEL_ID,
  APP_QUERY
} from '@/store/mutation-types.js'
import { headerParams } from '@/config/headerParams.js'

export default {
  onLaunch(options) {
	  const { query } = options
	  const { channel_id } = query
			
	  this.setSystemInfoSync()
	
	  // 设置渠道ID到本地数据中
	  const oldChannelId = storage.get(CHANNEL_ID)
	  const defaultChannelId = headerParams.pid
    console.log('旧渠道=', oldChannelId)
    console.log('默认渠道=', defaultChannelId)
	  if(oldChannelId && oldChannelId > 6000) {
	    // 数据库渠道ID从6000开始自动增长。已有推广渠道，保持推广渠道
	    console.log('已有推广渠道，保持推广渠道')
	  } else {
	    // 有新渠道并设置，否则设置默认渠道
	    const access = channel_id || defaultChannelId
	    storage.set(CHANNEL_ID, access)
	  }
	  
	  // 从抖音广告进入时，保存抖音广告参数
	  storage.set(APP_QUERY, query)
	
	  // 加载系统信息，安卓或iOS
	  this.$store.dispatch('SystemInfo')
	
	  //存在则关闭启动页进入首页	
	  // #ifdef APP-PLUS
	  plus.navigator.closeSplashscreen()
	  // #endif
  },
  onShow() {},
  onHide() {},
  methods: {
	  // 设置系统标识  0=未知 1=安卓 2=ios 3=微信小程序安卓 4=微信小程序ios 5=抖音小程序安卓 6=抖音小程序ios
    setSystemInfoSync() {
		  const systemInfoSync = uni.getSystemInfoSync()
		  const { osName } = systemInfoSync
		  if (osName === 'ios') storage.set(SYSTEM_CLIENT, 6)
		  else if (osName === 'android') storage.set(SYSTEM_CLIENT, 5)
		  else storage.set(SYSTEM_CLIENT, 0)
    }
  },
  onPageNotFound: [
    function(res) {
      // 跳转到404页面
      uni.redirectTo({ url: 'pages/error/404', })
    },
  ]
}
</script>

<style lang="scss">
	@import "@/uni_modules/uview-ui/index.scss";
	@import "@/static/style.scss";
	@import "@/static/copy-uview-theme.scss";
	@import "@/static/common.scss";
</style>
